<?php
/*****************************************************
 * Project: PPDAO
 * 
 * 
 * Author: Ivan Femia
 * Last modify by: Ivan Femia
 * 
 ***************************************************** 
 * 
 * Main controller
 * 
 * !!!!DO NOT CHANGE THIS FILE!!!!
 * 
 *****************************************************/

/* CONFIGURATION */
include_once '../../config/parameters.inc.php';

$parameters = new Parameters();

// Check post vars in order to identify the action
foreach ($_GET as $k => $v ) {
	$parameters->offsetSet( $k, new Parameter( $k, $v, Parameter::_GET ) );
}

foreach ($_POST as $k => $v ) {
	$parameters->offsetSet( $k, new Parameter( $k, $v, Parameter::_POST ) );
}

// TODO: Custom parameters if required
// $parameters->append ( new Parameter( $name, $value, Parameter::_CUSTOM ) );

$className = ucfirst($_GET['action'])."Action";
if ( class_exists($className,TRUE ) ) {
	// 1- instantiate the action class
	$actionClass = new $className;
	$actionClass->__request($parameters);

	$sessionManager = SessionManager::getInstance();
	
	// 2- Load objects from session	
	$objectsList = $actionClass->getObjectsList();
	
	$iterator = $objectsList->getIterator();
	while($iterator->valid()) {
	    $objectList = $iterator->current();
	    if ($objectList->getType == ObjectList::_PRIVATE) {
	   		$sessionName = $_GET['action'] . "_" . $objectList->getName();
	    } else {
	    	$sessionName = "common_" . $objectList->getName();
	    }
	    $objectList->setValue($sessionManager->getFromSession($sessionName));
	    $objectsList->offsetSet($iterator->key(), $objectList);
	    $iterator->next();
	}
	$actionClass->setObjectsList($objectsList);
	
	// 3- Execute the action
	$actionClass->__execute();
	
	// 4- Manage errors
	$sessionManager->addToSession( "restricted_errors", $actionClass->getErrors(), TRUE );
	
	// 5- Store objects in session
	$objectsList = $actionClass->getObjectsList();
	
	$iterator = $objectsList->getIterator();
	while($iterator->valid()) {
	    $objectList = $iterator->current();
	    if ($objectList->getType == ObjectList::_PRIVATE) {
	   		$sessionName = $_GET['action'] . "_" . $objectList->getName();
	    } else {
	    	$sessionName = "common_" . $objectList->getName();
	    }
	    $sessionManager->addToSession( $sessionName, $objectList->getValue(), TRUE );
	    $iterator->next();
	}
	
	// 6- Load the response	
	$response = $actionClass->__response();
	
	header("Location:../../../index.php?page=".$response."&lastAction=".$_GET['action']);
	
} else {
	throw new ClassNotFoundException( ucfirst($_GET['action']) );
}


/**
 * Autoload dynamically action classes
 */

function __autoload($_class) {
	
    include_once($_class . '.php');

    // Check to see whether the include declared the class
    if (!class_exists($_class, false)) {
        trigger_error("Unable to load class: $_class", E_USER_WARNING);
    }
    
    return true;
}	

?>
